home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0144_Search window list for all Windows title.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  1.6 KB  |  52 lines

  1.  
  2. function FindMatchingWindows(SearchFor: string; AtBeginning: boolean; var FoundWindows: TStringList): longint;
  3.  
  4. {Search window list for all Windows where the title contains or begins
  5.  with (AtBeginning) SearchFor. Returns a TStringList with the title of the
  6.  found windows as string and the window-Handle as Object.
  7.  It is up to you to create and free the TStringList}
  8.  
  9. var
  10.   hWndFirst, hWndCurWin, hWndDesk: HWnd;
  11.   szWinText: pChar;
  12.   WinText: string;
  13.   foundAt: byte;
  14. begin
  15.   hWndDesk:= GetDesktopWindow;
  16.   {This ist the parent of alle top-level windows}
  17.   if hWndDesk <> 0 then
  18.         hWndCurWin := GetWindow(hWndDesk,GW_CHILD)
  19.   else
  20.         {place error handling here}
  21.         exit;
  22.   if not assigned(FoundWindows) then
  23.         {you have to create Stringlist before passing the
  24.          variable to this function}
  25.         exit;
  26.   getMem(szWinText,256);
  27.   hWndFirst:= hWndCurWin;
  28.   while (hWndCurWin <> 0) do
  29.   begin
  30.     GetWindowText(hWndCurWin, szWinText,255);
  31.     WinText:= strpas(szWinText);
  32.     if SearchFor = '' then
  33.     begin
  34.         if WinText = '' then Wintext := format ('Fenster Nr. %d (Ohne Titel)',[hWndCurWin]);
  35.         FoundWindows.addObject(WinText,TObject(hWndCurWin))
  36.     end
  37.     else
  38.     begin
  39.       foundAt:= pos(SearchFor, WinText);
  40.       if (not atBeginning and (foundAt > 0)) or (foundAt = 1) then
  41.       begin
  42.         FoundWindows.addObject(WinText,TObject(hWndCurWin));
  43.       end;
  44.     end;
  45.     hWndCurWin := GetWindow(hWndCurWin,GW_HWNDNEXT);
  46.   end;
  47.   freeMem(szWinText,256);
  48.   result := FoundWindows.count;
  49. end;
  50.  
  51.  
  52.